package com.tzbank.wisdom.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.utils.ChineseCharToEn;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.service.PayeeService;
import com.tzbank.wisdom.utils.BusinessConstant;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;



@Service(value = "PayeeAddService")
@Slf4j
public class PayeeServiceImpl implements PayeeService {
	
	/**
	 * 
	 * 添加收款人
	 *
	 */


	public Map<String, Object> payeeUpdate(Map<String, String> reqBody) {
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead =RequestMap.getReqSysHead(
				TransCodeEnum.PAYEE_UPDATE.getServiceCode(),TransCodeEnum.PAYEE_UPDATE.getServiceSence(), 
				   "", 
				   BusinessConstant.SOURCRE_TYPE_PMBBS);
/*		reqSysHead.put("clientNo", "1008905143");
		reqSysHead.put("eClientNo", "500000040");*/
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
		inputMap.putAll(userMap);
		//调用核心计价因子入参 
		inputMap.put("sysHead", reqSysHead);
		//(payeeType) 行内转账I、跨行转账D、他行快速转账P、信用卡还款T
	
		//原来的收款人信息（origPayeeAcctNo,oriPayeeType,payeeNo） 必传
		inputMap.put("origPayeeAcctNo", reqBody.get("origPayeeAcctNo")); //原收款人账号
		inputMap.put("oriPayeeType", reqBody.get("oriPayeeType"));//原收款人PAYEE_TYPEMOBILE
		inputMap.put("payeeNo", reqBody.get("payeeNo"));//原收款人定向标志
		//原来的收款人结束
		//要修改的内容
		inputMap.put("payeeType", reqBody.get("payeeType"));        //行内转账I、跨行转账D、他行快速转账P、信用卡还款T
		inputMap.put("payeeAcctNo", reqBody.get("payeeAcctNo")); //收款人账号
		inputMap.put("payeeName", reqBody.get("payeeName"));//收款人姓名
	
		inputMap.put("payeeOpenBranch", reqBody.get("payeeOpenBranch"));//收款人开户行行号
		inputMap.put("payeeBankName", reqBody.get("payeeBankName"));//收款人开户行名称
		inputMap.put("provinceCode", reqBody.get("provinceCode"));//省代码
		inputMap.put("provinceName", reqBody.get("provinceName"));//省名称
		inputMap.put("payeeCityCode", reqBody.get("payeeCityCode"));//收款人城市代码
		inputMap.put("payeeCityName", reqBody.get("payeeCityName"));//收款人城市名称
		inputMap.put("subBranchId", reqBody.get("subBranchId"));//网点号
		inputMap.put("subBranchName", reqBody.get("subBranchName"));//网点名称
		inputMap.put("payeeSettleBank", reqBody.get("payeeSettleBank"));//收款清算行行号
		inputMap.put("unionBankCode", reqBody.get("unionBankCode"));//联合机构号
		inputMap.put("mobile", reqBody.get("mobile"));//手机号码
		inputMap.put("fastFlag", reqBody.get("fastFlag"));//快速标志(转账关系)
		inputMap.put("limit", reqBody.get("limit"));//限额
		inputMap.put("acctAlias", reqBody.get("acctAlias"));//账户别名
		inputMap.put("tokenPwd", reqBody.get("tokenPwd"));//口令密码
		//inputMap.put("fastFlag", "B");//原收款人定向标志
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 添加收款人结果

		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					result.get("data"));
		}
		return ResultUtil.resultMap( ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
	}
	/**
	 * 
	 * 修改收款人
	 *
	 */
	@Override
	public Map<String, Object> payeeDelete(Map<String, String> reqBody) {
		

		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
			Map<String, Object> reqSysHead =RequestMap.getReqSysHead(
					TransCodeEnum.PAYEE_DELETE.getServiceCode(),TransCodeEnum.PAYEE_DELETE.getServiceSence(), 
					   "", 
					   BusinessConstant.SOURCRE_TYPE_PMBBS);	
			Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
			if (userMap.get("clientNo")==null) {
				return userMap;
			}	
		reqSysHead.putAll(userMap);
		//调用核心计价因子入参 PAYEE_ACCT_NO SOURCECHANEL
		inputMap.put("sysHead", reqSysHead);
		//inputMap.put("payeeType", "I");        //行内转账I、跨行转账D、他行快速转账P、信用卡还款T
		inputMap.put("payeeNo", reqBody.get("payeeNo")); //收款人账号 
		inputMap.put("payeeType", reqBody.get("payeeType")); 
		inputMap.put("payeeAcctNo", reqBody.get("payeeAcctNo"));
		inputMap.put("fastFlag", reqBody.get("fastFlag")); 
		
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 修改收款人结果
		
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					result.get("data"));
		}
		return ResultUtil.resultMap( ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
	}
	@Override
	public Map<String, Object> payeeQuery(Map<String, String> reqBody) {
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead =RequestMap.getReqSysHead(
				TransCodeEnum.PAYEE_QUERY.getServiceCode(),TransCodeEnum.PAYEE_QUERY.getServiceSence(), 
				   "", 
				   BusinessConstant.SOURCRE_TYPE_PMBBS);
			Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
			if (userMap.get("clientNo")==null) {
				return userMap;
			}
		 reqSysHead.putAll(userMap);

		inputMap.put("sysHead", reqSysHead);
		//inputMap.put("payeeType", "I");        //行内转账I、跨行转账D、他行快速转账P、信用卡还款T
		inputMap.put("payeeAcctNo", reqBody.get("payeeAcctNo")); //收款人账号
		inputMap.put("payeeName", reqBody.get("payeeName"));//
		inputMap.put("payeeCityCode", reqBody.get("payeeCityCode"));//收款人城市代码
		inputMap.put("payeeAcctType", reqBody.get("payeeAcctType"));//收款人账户类型
		inputMap.put("tranFlag", reqBody.get("tranFlag"));//交易标志
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 修改收款人结果
		log.info("响应"+JSONObject.toJSONString(result));
		Map<String, Object>  data = (Map<String, Object>) result.get("data");   
		Map<String, Object> realDate =  new HashMap<String, Object>();
		List  listArray  =  (List) data.get("listArray");
		//对参数进行整理   根据名字首字母进行分组
		for( int i =0 ;i<listArray.size();i++) {
			 Map<String, Object> params = (Map<String, Object>) listArray.get(i);
			 List  resultArray1= new ArrayList();	
			 String payeeName=params.get("payeeName")+"".trim();
			 
			 if (!isInteger(payeeName)&&ChineseCharToEn.conversionStr(payeeName, "GB2312", "ISO8859-1").length() > 1) {
				 realDate.put(ChineseCharToEn.getFirstLetter(payeeName), resultArray1);
				 params.put("firstLetter", ChineseCharToEn.getFirstLetter(payeeName));
			 }
		}
		
		List l=new ArrayList();//组装新的参数
		for(Map.Entry<String, Object> entry:realDate.entrySet()) {
			
			Map m=new HashMap();
			m.put("key", entry.getKey());
			List  d= new ArrayList();
			for( int i =0 ;i<listArray.size();i++) {
				 Map<String, Object> params = (Map<String, Object>) listArray.get(i);
				 if(entry.getKey().equals(params.get("firstLetter")+"".trim())) {
					 d.add(params);
					 m.put("content", d);
				 }
			}
			l.add(m);
		}	
		
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					result.get("data"));
		}
		Map<String, Object> resultMap=new HashMap<String, Object>();
		resultMap.put("resultList", l);
		return ResultUtil.resultMap( ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(), resultMap);
	}

	@Override
	public Map<String, Object> payeeAdd(Map<String, String> reqBody) {
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead =RequestMap.getReqSysHead(
				TransCodeEnum.PAYEE_ADD.getServiceCode(),TransCodeEnum.PAYEE_ADD.getServiceSence(), 
				   "", 
				   BusinessConstant.SOURCRE_TYPE_PMBBS);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo")==null) {
			return userMap;
		}	
		reqSysHead.putAll(userMap);
		//调用核心计价因子入参 
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("payeeAcctType", reqBody.get("payeeAcctType"));//收款人账户类型        //行内转账I、跨行转账D、他行快速转账P、信用卡还款T
		inputMap.put("payeeAcctNo", reqBody.get("payeeAcctNo"));//收款人账号
		inputMap.put("payeeName", reqBody.get("payeeName"));//收款人姓名
		inputMap.put("payeeOpenBranch", reqBody.get("payeeOpenBranch"));//收款人开户行行号
		inputMap.put("payeeBankName", reqBody.get("payeeBankName"));//收款人开户行名称
		inputMap.put("provinceCode", reqBody.get("provinceCode"));//省代码
		inputMap.put("provinceName", reqBody.get("provinceName"));//省名称
		inputMap.put("payeeCityCode", reqBody.get("payeeCityCode"));//收款人城市代码
		inputMap.put("payeeCityName", reqBody.get("payeeCityName"));//收款人城市名称
		inputMap.put("subBranchId", reqBody.get("subBranchId"));//网点号
		inputMap.put("subBranchName", reqBody.get("subBranchName"));//网点名称
		inputMap.put("payeeSettleBank", reqBody.get("payeeSettleBank"));//收款清算行行号
		inputMap.put("unionBankCode", reqBody.get("unionBankCode"));//联合机构号
		inputMap.put("mobile", reqBody.get("mobile"));//手机号码
		inputMap.put("fastFlag", reqBody.get("fastFlag"));//快速标志(转账关系)
		inputMap.put("limit", reqBody.get("limit"));//限额
		inputMap.put("acctAlias", reqBody.get("acctAlias"));//账户别名
		inputMap.put("tokenPwd", reqBody.get("tokenPwd"));//口令密码
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 添加收款人结果
		
		log.info("响应"+JSONObject.toJSONString(result));
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					result.get("data"));
		}
		return ResultUtil.resultMap( ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
	}

	 public static boolean isInteger(String str) {  
	        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
	        return pattern.matcher(str).matches();  
	  }	
}